package person.zjh.kaoyansharehub.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import person.zjh.kaoyansharehub.dto.PageCommentDTO;
import person.zjh.kaoyansharehub.dto.SaveCommentDTO;
import person.zjh.kaoyansharehub.mapper.CommentMapper;
import person.zjh.kaoyansharehub.mapper.ReplyMapper;
import person.zjh.kaoyansharehub.mapper.UserMapper;
import person.zjh.kaoyansharehub.pojo.Comment;
import person.zjh.kaoyansharehub.pojo.Reply;
import person.zjh.kaoyansharehub.pojo.User;
import person.zjh.kaoyansharehub.result.PageResult;
import person.zjh.kaoyansharehub.result.Result;
import person.zjh.kaoyansharehub.service.CommentService;
import person.zjh.kaoyansharehub.vo.CommentVO;

import java.util.List;

/**
 * @description: TODO
 * @author 郑佳豪
 * @date 2025/2/20 14:13
 * @version 1.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    private final CommentMapper commentMapper;
    private final UserMapper userMapper;
    private final ReplyMapper replyMapper;


    @Override
    public PageResult getComment(PageCommentDTO requestParam) {
        PageHelper.startPage(requestParam.getPageNum(),requestParam.getPageSize());
        List<Comment> commentList = commentMapper.getComment(requestParam);

        PageInfo<Comment> pageInfo = new PageInfo<>(commentList);

        List<CommentVO> commentVOList = BeanUtil.copyToList(commentList, CommentVO.class);
        for (CommentVO commentVO : commentVOList) {
            User user = userMapper.selectById(commentVO.getUserId());
            commentVO.setUserAvatarPath(user.getAvatar());
            commentVO.setUserName(user.getUserName());
        }

        for (CommentVO commentVO : commentVOList) {
            LambdaQueryWrapper<Reply> wrapper = new LambdaQueryWrapper<Reply>()
                    .eq(Reply::getCommentId, commentVO.getCommentId());
            commentVO.setReplyCount(replyMapper.selectCount(wrapper));
        }


        log.info("总条数：{}",pageInfo.getTotal() );
        return new PageResult(pageInfo.getTotal(),commentVOList);
    }

    @Override
    public Result saveComment(SaveCommentDTO requestParam) {
        try {
            Comment comment = BeanUtil.copyProperties(requestParam, Comment.class);
            save(comment);
            return Result.success();
        } catch (Exception e) {
            log.info("保存评论失败");
            e.printStackTrace();
            return Result.error("发布评论失败");
        }

    }

    @Override
    public Result getCommentCountByUserId(Long userId) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<Comment>()
                .eq(Comment::getUserId, userId);

        Long count = commentMapper.selectCount(wrapper);
        return Result.success(count);
    }

    @Override
    public void deleteComment(String commentId) {
        removeById(commentId);
    }


}
